import 'dart:convert';

import 'package:shared_preferences/shared_preferences.dart';

class SpUtils{

  static final SpUtils instance = SpUtils._();
  factory SpUtils() => instance;
  static SharedPreferences? _prefs;
  SpUtils._(){
    _init();
  }

  Future _init() async {
    _prefs = await SharedPreferences.getInstance();
  }

  static Future<void>? reload() {
    return _prefs?.reload();
  }

  static bool isInitialized() {
    return _prefs != null;
  }

  static SharedPreferences? getSp() {
    return _prefs;
  }

  /**
   *
   */
  static Future<bool>? putObject(String key, Object value) {
    return _prefs?.setString(key, json.encode(value));
  }

  static T? getObj<T>(String key, T f(Map v), {T? defValue}) {
    Map? map = getObject(key);
    return map == null ? defValue : f(map);
  }

  static Map? getObject(String key) {
    String? _data = _prefs?.getString(key);
    return (_data == null || _data.isEmpty) ? null : json.decode(_data);
  }

  static Future<bool>? putObjectList(String key, List<Object> list) {
    List<String>? _dataList = list.map((value) {
      return json.encode(value);
    }).toList();
    return _prefs?.setStringList(key, _dataList);
  }

  static List<T>? getObjList<T>(String key, T f(Map v),
      {List<T>? defValue = const []}) {
    List<Map>? dataList = getObjectList(key);
    List<T>? list = dataList?.map((value) {
      return f(value);
    }).toList();
    return list ?? defValue;
  }

  static List<Map>? getObjectList(String key) {
    List<String>? dataLis = _prefs?.getStringList(key);
    return dataLis?.map((value) {
      Map _dataMap = json.decode(value);
      return _dataMap;
    }).toList();
  }

  static String? getString(String key, {String? defValue = ''}) {
    return _prefs?.getString(key) ?? defValue;
  }

  static Future<bool>? putString(String key, String value) {
    return _prefs?.setString(key, value);
  }

  static bool? getBool(String key, {bool? defValue = false}) {
    return _prefs?.getBool(key) ?? defValue;
  }

  static Future<bool>? putBool(String key, bool value) {
    return _prefs?.setBool(key, value);
  }

  static int? getInt(String key, {int? defValue = 0}) {
    return _prefs?.getInt(key) ?? defValue;
  }

  static Future<bool>? putInt(String key, int value) {
    return _prefs?.setInt(key, value);
  }

  static double? getDouble(String key, {double? defValue = 0.0}) {
    return _prefs?.getDouble(key) ?? defValue;
  }

  static Future<bool>? putDouble(String key, double value) {
    return _prefs?.setDouble(key, value);
  }

  static List<String>? getStringList(String key,
      {List<String>? defValue = const []}) {
    return _prefs?.getStringList(key) ?? defValue;
  }

  static Future<bool>? putStringList(String key, List<String> value) {
    return _prefs?.setStringList(key, value);
  }

  static dynamic getDynamic(String key, {Object? defValue}) {
    return _prefs?.get(key) ?? defValue;
  }

  static bool? haveKey(String key) {
    return _prefs?.getKeys().contains(key);
  }

  static bool? containsKey(String key) {
    return _prefs?.containsKey(key);
  }

  static Set<String>? getKeys() {
    return _prefs?.getKeys();
  }

  static Future<bool>? remove(String key) {
    return _prefs?.remove(key);
  }

  static Future<bool>? clear() {
    return _prefs?.clear();
  }

}